package com.tool.imagemonitor;

import java.util.ArrayList;
import java.util.List;

import org.come.Frame.MsgJframe5;
import org.come.Frame.ZhuFrame;
import org.come.bean.FightOperation;
import org.come.entity.Goodstable;
import org.come.good.Consumptions;
import org.come.mouslisten.GoodsMouslisten;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.FormsManagement;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.GsonUtil;
import org.come.until.MessagrFlagUntil;

import come.tool.Fighting.FightingEvents;
import come.tool.Fighting.FightingMixDeal;
import come.tool.Fighting.FightingState;
import come.tool.Fighting.Fightingimage;
import come.tool.handle.HandleState;
/**
 * 战斗图像监听
 * @author Administrator
 *
 */
public class FightingMonitor {
	//人的操作记录
	public static FightOperation RoleOperation=new FightOperation();
	//召唤兽的操作记录
	public static FightOperation PetOperation=new FightOperation();;	
	public static int mousestate=0;//鼠标状态  0普通攻击(默认) 1法术攻击 2物品使用 3保护 4捕捉
	//战斗点击的类型
	public static String mousesname="普通攻击";
	public static void Fighting(Fightingimage fightingimage) {
	// TODO Auto-generated constructor stub
		int man=FightingMixDeal.myman();
		if (FightingMixDeal.State==HandleState.HANDLE_PET) {
            man+=5;
        }
		if (mousestate==0) {
			if (DecisionAttack(man, fightingimage)) {
				FightOperation operation=FightingMonitor.getOperation();
				operation.Record(fightingimage.getFightingManData().getCamp(),fightingimage.getFightingManData().getMan(),mousestate, null);
				FightingMonitor.execution(operation);	
	        }
		}else if (mousestate==1) {
			if ("一矢中的".equals(mousesname)) {
				// 只能对人物单位使用
				if (ziji(man, fightingimage) || fightingimage.getFightingManData().getMan() > 4) {
					return;
				}
			}
			if (DecisionSpell(mousesname, man, fightingimage)) {
				FightOperation operation=FightingMonitor.getOperation();
				operation.Record(fightingimage.getFightingManData().getCamp(),fightingimage.getFightingManData().getMan(),mousestate, mousesname);
				FightingMonitor.execution(operation);	
			}
		}else if (mousestate==2) {
			FightOperation operation=FightingMonitor.getOperation();
			operation.Record(fightingimage.getFightingManData().getCamp(),fightingimage.getFightingManData().getMan(),mousestate, mousesname);
			FightingMonitor.execution(operation);	
		}else if (mousestate==3) {
			if (youfang(fightingimage)) {
				if (!ziji(man, fightingimage)) {
					FightOperation operation=FightingMonitor.getOperation();
					operation.Record(fightingimage.getFightingManData().getCamp(),fightingimage.getFightingManData().getMan(),mousestate,null);
					FightingMonitor.execution(operation);	
				}
			}
		}else if (mousestate==4) {
			//判断是否是敌方且战斗类型是0
			if ((FightingMixDeal.BattleType==0||FightingMixDeal.BattleType==1||FightingMixDeal.BattleType==2)&&!youfang(fightingimage)) {
				FightOperation operation=FightingMonitor.getOperation();
				operation.Record(fightingimage.getFightingManData().getCamp(),fightingimage.getFightingManData().getMan(),mousestate,null);
				FightingMonitor.execution(operation);	
			}
		}
    }
	/**判断是否是友方*/
	public static boolean youfang(Fightingimage fightingimage){
		if (fightingimage.getFightingManData().getCamp()==FightingMixDeal.camp) {
				return true;
		}
		return false;
	}
	/**判断点击的人是否有隐身状态*/
	public static boolean yinshen(Fightingimage fightingimage){
		return fightingimage.getFightingManData().getAlpha()==1.0f?true:false;
	}
	/**判断点击的人是否是自己*/
	public static boolean ziji(int man,Fightingimage fightingimage){
		if (fightingimage.getFightingManData().getCamp()==FightingMixDeal.camp
				&&fightingimage.getFightingManData().getMan()==man) {
			return true;
		}
		return false;
	}
	/**普通攻击对象判断*/
	public static boolean DecisionAttack(int man,Fightingimage fightingimage){
		if (yinshen(fightingimage)&&!ziji(man, fightingimage)) {
			return true;
		}
		return false;
	}
	/**法术攻击对象判断*/
	public static boolean DecisionSpell(String skillname,int man,Fightingimage fightingimage){
		if (skillname.equals("移花接木")||skillname.equals("大手印")|| skillname.equals("荆棘束身") || skillname.equals("覆雨") || skillname.equals("御龙")|| skillname.equals("引火烧身")|| skillname.equals("银索金铃"))return true;
		if (Effect(skillname)) {
			if(skillname.equals("流连忘返")){
				return true;
			}else{
				if (youfang(fightingimage)) return true;
				else return false;
			}
		}else {
			if (!youfang(fightingimage)/*&&yinshen(fightingimage)*/) return true;//修改隐身状态不能被法术点击
			if (Effect(skillname)) {
				if(skillname.equals("流连忘返") ){
					return true;
				}else{
					if (youfang(fightingimage)) return true;
					else return false;
				}
			} else {
				if (!youfang(fightingimage) && yinshen(fightingimage)) {
					return true;
				}
				else return false;
			}
		}

	}

	/**判断该法术是否可以作用于友方*/
	public static boolean Effect(String skillname) {
		if ("绝境逢生".equals(skillname) || "子虚乌有".equals(skillname)
				|| "春回大地".equals(skillname) || "妙手回春".equals(skillname)
				|| "春意盎然".equals(skillname) || "忠诚".equals(skillname)
				|| "自医".equals(skillname) || "遗产".equals(skillname)
				|| "隐身".equals(skillname) || "仁之木叶神".equals(skillname)
				|| "智之水叶神".equals(skillname) || "忠诚".equals(skillname)
				|| "礼之火叶神".equals(skillname) || "大隐于朝".equals(skillname)
				|| "鬼神莫测".equals(skillname) || "神出鬼没".equals(skillname)
				|| "作鸟兽散".equals(skillname) || "将死".equals(skillname)
				|| "妖之魔力".equals(skillname) || "力神复苏".equals(skillname)
				|| "红袖添香".equals(skillname) || "莲步轻舞".equals(skillname)
				|| "魔之飞步".equals(skillname) || "急速之魔".equals(skillname)
				|| "幽怜魅影".equals(skillname) || "醉生梦死".equals(skillname)
				|| "狮王之怒".equals(skillname) || "兽王神力".equals(skillname)
				|| "魔神附身".equals(skillname) || "楚楚可怜".equals(skillname)
				|| "魔神护体".equals(skillname) || "含情脉脉".equals(skillname)
				|| "魔神飞舞".equals(skillname) || "天外飞魔".equals(skillname)
				|| "乾坤借速".equals(skillname) || "一曲销魂".equals(skillname)
				|| "秦丝冰雾".equals(skillname) || "倩女幽魂".equals(skillname)
				|| "泽披天下".equals(skillname) || "将军令".equals(skillname)
				|| "大势锤".equals(skillname) || "七宝玲珑塔".equals(skillname)
				|| "黑龙珠".equals(skillname) || "幽冥鬼手".equals(skillname)
				|| "绝情鞭".equals(skillname) || "宝莲灯".equals(skillname)
				|| "番天印".equals(skillname) || "锦襕袈裟".equals(skillname)
				|| "银索金铃".equals(skillname) || "飞花溅玉".equals(skillname)
				|| "百害不侵".equals(skillname) || "明心见性".equals(skillname)
				|| "五蕴炽盛".equals(skillname) || "清江映雪".equals(skillname)
				|| "回光返照".equals(skillname) || "吹箫引凤".equals(skillname)
				|| "六根清净".equals(skillname) || "因缘际会".equals(skillname)
				|| "以直报怨".equals(skillname) || "如有神助".equals(skillname)
				|| "破釜沉舟".equals(skillname) || "弱水三千".equals(skillname)
				|| "流风回雪".equals(skillname) || "岳镇渊渟".equals(skillname)
				|| "穿针引线".equals(skillname) || "鸿雁长飞".equals(skillname)
				|| "销声匿迹".equals(skillname) || "一苇渡江".equals(skillname)
				|| "苦海慈航".equals(skillname) || "拔山".equals(skillname)
				|| "七星贯日".equals(skillname) || "御龙".equals(skillname)
				|| "覆雨".equals(skillname) || "御龙".equals(skillname)
				|| "引火烧身".equals(skillname) || "久旱初雨".equals(skillname)
				|| "兴云致雨".equals(skillname) || "润物无声".equals(skillname)
				|| "沛然莫御".equals(skillname) || "泽被万物".equals(skillname)
				|| "春暖花开".equals(skillname)|| "瞒天过海".equals(skillname)
				|| "双生两仪盾".equals(skillname) || "一矢中的".equals(skillname)
				|| "凝神一击".equals(skillname) || "藏锋蓄势".equals(skillname)
				|| "法魂护体".equals(skillname)|| "幻影迷踪".equals(skillname)
				|| "兽魂俯首".equals(skillname)|| "刚柔兼备".equals(skillname)
				|| "偷梁换柱".equals(skillname)|| "遮天蔽日".equals(skillname)|| skillname.equals("流连忘返")) {
			return true;
		}
		return false;
	}	
	/**向服务器发送战斗操作*/
	public static void FightingOperation(FightingEvents fightingEvents) {
		if (FightingMixDeal.camp == -1) {
            return;
        }
		fightingEvents.setFightingsum(FightingMixDeal.FightingNumber);
		fightingEvents.setCurrentRound(FightingMixDeal.CurrentRound);

		String sendMes = Agreement.getAgreement().battleroundAgreement(GsonUtil.getGsonUtil().getgson().toJson(fightingEvents));
		SendMessageUntil.toServer(sendMes);
		FormsManagement.HideForm(46);
		FormsManagement.HideForm(34);
		FormsManagement.HideForm(631);
		FormsManagement.HideForm(6);
		FormsManagement.HideForm(40);
	}
	public static FightingEvents CheHuiFightingEvents() {
		FightingEvents fEvents=new FightingEvents();

		fEvents.setOriginator(FightingMixDeal.FightingState("撤回", 0));

		return fEvents;
	}
	public static void execution(FightOperation Operation){
		execution(Operation, false);
	}
	//执行操作 true 是自动
	public static void execution(FightOperation Operation,boolean is){
		if (FightingMixDeal.State == 1) {
			if (!is && Operation.getType() != 6 && Operation.getType() != 5 && FightingMixDeal.isLL()) {
				return;
			}
		}
		else {
			if (FightingMixDeal.State != 2) {
				return;
			}
		}
		final FightingEvents events = AttackGenerate(Operation);
		if (Operation.getType() == 1) {
			Consumptions.addSkillProficiency(Operation.getSpell(), 1);
		}
		if (FightingMixDeal.State == 1) {
			if (Operation.getType() == 1) {
				MsgJframe5.getMsJframe5().getJapnel5().sublime = Operation.getSpell();
			}
			else {
				if (Operation.getType() == 0) {
					MsgJframe5.getMsJframe5().getJapnel5().sublime = FightingMonitor.mousesname;
				}
				else {
					if (Operation.getType() == 5) {
						MsgJframe5.getMsJframe5().getJapnel5().sublime = "防御";
					}
					else {
						if (Operation.getType() == 4) {
							MsgJframe5.getMsJframe5().getJapnel5().sublime = "捕捉";
						}
						else {
							if (Operation.getType() == 6) {
								MsgJframe5.getMsJframe5().getJapnel5().sublime = "逃跑";
							}
							else {
								if (Operation.getType() == 7) {
									MsgJframe5.getMsJframe5().getJapnel5().sublime = "召回";
								}
								else {
									if (Operation.getType() == 2) {
										MsgJframe5.getMsJframe5().getJapnel5().sublime = "物品使用";
									}
									else {
										if (Operation.getType() == 3) {
											MsgJframe5.getMsJframe5().getJapnel5().sublime = "保护";
										}
									}
								}
							}
						}
					}
				}
			}
		}
		else {
			if (Operation.getType() == 1) {
				MsgJframe5.getMsJframe5().getJapnel5().subsumpet = Operation.getSpell();
			}
			else {
				if (Operation.getType() == 0) {
					MsgJframe5.getMsJframe5().getJapnel5().subsumpet = FightingMonitor.mousesname;
				}
				else {
					if (Operation.getType() == 5) {
						MsgJframe5.getMsJframe5().getJapnel5().subsumpet = "防御";
					}
					else {
						if (Operation.getType() == 6) {
							MsgJframe5.getMsJframe5().getJapnel5().sublime = "逃跑";
						}
						else {
							if (Operation.getType() == 2) {
								MsgJframe5.getMsJframe5().getJapnel5().subsumpet = "物品使用";
							}
							else {
								if (Operation.getType() == 3) {
									MsgJframe5.getMsJframe5().getJapnel5().subsumpet = "保护";
								}
							}
						}
					}
				}
			}
		}
		FightingOperation(events);
		operateEnd();
	}
	/**点击生效处理*/
	public static void operateEnd(){
		MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
		 mousestate=0;
		 mousesname="普通攻击";
		 if (FightingMixDeal.State==HandleState.HANDLE_PLAYER&&FightingMixDeal.MyBeastLifeAndDeath()) {
			FightingMixDeal.changeState(HandleState.HANDLE_PET);
			ZhuFrame.getZhuJpanel().HideBeastBtn();
			ZhuFrame.getZhuJpanel().ShowBeastBtn();	
		 }else{	
			FightingMixDeal.changeState(HandleState.HANDLE_WAIT);
			FightingMixDeal.RoundFighting();		 
		 }
	}	
	//获取当前是人是兽
	public static FightOperation getOperation(){
		if (FightingMixDeal.State==HandleState.HANDLE_PLAYER) {
			return RoleOperation;   
		}else {
		    return PetOperation;
		}
	}
	   //生成战斗指令
	   public static FightingEvents AttackGenerate(FightOperation operation){
			FightingEvents fEvents=new FightingEvents();
			List<FightingState> fList=new ArrayList<>();
		    int type=0;
		    if (FightingMixDeal.State==HandleState.HANDLE_PET) {
		    	type=1;
			}
		    fEvents.setOriginator(FightingMixDeal.FightingState("普通攻击", type));
		    //当前被打人物	
		    switch (operation.getType()) {
			case 1:
				fEvents.getOriginator().setStartState("技能");
				fEvents.getOriginator().setEndState(operation.getSpell());
				break;
			case 2:
				Goodstable goodstable=GoodsListFromServerUntil.getRgid(operation.getGood());
				if (goodstable!=null && goodstable.getGoodlock() != 1 && (goodstable.getType() == 0 || goodstable.getType() == 1)) {
					fEvents.getOriginator().setStartState("药");
					fEvents.getOriginator().setEndState(goodstable.getValue());	
					goodstable.goodxh(1);
					GoodsMouslisten.gooduse(goodstable,1);
					if (goodstable.getUsetime()<=0){
						GoodsListFromServerUntil.Deletebiaoid(goodstable.getRgid());
						operation.setCamp(-1);
						operation.setMan(-1);
						operation.setType(0);
					}
				}else {
					operation.setCamp(-1);
					operation.setMan(-1);
				}
				break;
			case 3:
				fEvents.getOriginator().setStartState("保护");
				break;
			case 4:
				fEvents.getOriginator().setStartState("捕捉");
				break;
			case 5:
				fEvents.getOriginator().setStartState("防御");
				break;
			case 6:
				fEvents.getOriginator().setStartState("逃跑");
				break;
			}
			FightingState dqstate=new FightingState(operation.getCamp(),operation.getMan(),null);
			fList.add(dqstate);
			fEvents.setAccepterlist(fList);
			return fEvents;		
	   }
	static {
		RoleOperation = new FightOperation();
		PetOperation = new FightOperation();
		mousestate = 0;
		mousesname = "普通攻击";
	}
}
